<?php
namespace  App\Http\Controllers\Backend;
use App\Constant\CacheConstant;
use App\Exceptions\CompanyApiException;
use App\Http\Controllers\Backend\Traits\ResponseTrait;
use App\Http\Controllers\Controller;
use App\Services\Base\Interfaces\CacheServiceInterface;
use App\Services\Other\Models\WechatPlatform;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Auth;


class BaseController extends Controller
{

    use ResponseTrait;
    const GUARD = 'apiv2';

    /**
     * @param string $message
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function success($message = '')
    {
        return response()->json([
            'code'  => 0,
            'message' => $message,
            'data'    => [],
        ]);
    }

    /**
     * @param array $data
     * @param string $message
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function successData($data = [], $message = '')
    {
        return response()->json([
            'code'  => 0,
            'message' => $message,
            'data'    => $data,
        ]);
    }

    /**
     * @param $message
     * @param int $code
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function error($message, $code = 1)
    {
        return response()->json([
            'code'  => $code,
            'message' => $message,
            'data'    => [],
        ]);
    }


    /**
     * 返回登录的公司id
     * @return int
     */
    protected function loginCompanyId()
    {
        $user =  Auth::guard(self::GUARD)->user();

        return  $user['login_company_id'];
    }

    /**
     * @return int
     */
    protected function id()
    {
        return Auth::guard(self::GUARD)->id();
    }

    protected function check(): bool
    {
        return Auth::guard(self::GUARD)->check();
    }

    protected function paginator($list, $total, $page, $pageSize)
    {
        return new LengthAwarePaginator($list, $total, $pageSize, $page, ['path' => request()->path()]);
    }
    protected function mobileCodeCheck()
    {
        $mobile = request()->input('mobile');
        $mobileCode = request()->input('mobile_code');

        if (mobile_code_check($mobile, $mobileCode) === false) {
            throw new  CompanyApiException(__('短信验证失败'));
        }
    }
    public function downFile(string $url, string $file_name)
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.android.package-archive');
        header('Content-Disposition: attachment; filename=' . $file_name);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $buffer) {
            echo $buffer;
            return strlen($buffer);
        });
        curl_exec($ch);
        curl_close($ch);
    }
    public function getCompanyId($id)
    {
        if (is_numeric($id)) {
            return $id;
        }
        $cacheService = app()->make(CacheServiceInterface::class);
        $cacheKey = get_cache_key(CacheConstant::WECHAT_MINI_APP['name'], $id);
        if ($cacheService->has($cacheKey)) {
            return $cacheService->get($cacheKey);
        }
        $mini = WechatPlatform::query()->where('app_id', $id)->first();
        $companyId = $mini['company_id'] ?? 1;
        $cacheService->put(get_cache_key(CacheConstant::WECHAT_MINI_APP['name'], $id), $companyId, CacheConstant::WECHAT_MINI_APP['expire']);
        return $companyId;

    }

}
